package com.ruoyi.jksd.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.jksd.common.JksdUtils;
import com.ruoyi.jksd.common.dto.XyDepositDto;
import com.ruoyi.jksd.domain.XyBalanceLog;
import com.ruoyi.jksd.domain.XyBankinfo;
import com.ruoyi.jksd.domain.XyUsers;
import com.ruoyi.jksd.service.IXyBalanceLogService;
import com.ruoyi.jksd.service.IXyBankinfoService;
import com.ruoyi.jksd.service.IXyUsersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jksd.mapper.XyDepositMapper;
import com.ruoyi.jksd.domain.XyDeposit;
import com.ruoyi.jksd.service.IXyDepositService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员-余额提现Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@Service
public class XyDepositServiceImpl implements IXyDepositService {
    @Autowired
    private XyDepositMapper xyDepositMapper;

    @Autowired
    IXyUsersService xyUsersService;

    @Autowired
    IXyBankinfoService bankinfoService;
    @Autowired
    IXyBalanceLogService xyBalanceLogService;

    /**
     * 查询会员-余额提现
     *
     * @param id 会员-余额提现主键
     * @return 会员-余额提现
     */
    @Override
    public XyDeposit selectXyDepositById(String id) {
        return xyDepositMapper.selectXyDepositById(id);
    }

    /**
     * 查询会员-余额提现列表
     *
     * @param xyDeposit 会员-余额提现
     * @return 会员-余额提现
     */
    @Override
    public List<XyDeposit> selectXyDepositList(XyDeposit xyDeposit) {
        return xyDepositMapper.selectXyDepositList(xyDeposit);
    }

    /**
     * 新增会员-余额提现
     *
     * @param xyDeposit 会员-余额提现
     * @return 结果
     */
    @Override
    public int insertXyDeposit(XyDeposit xyDeposit) {
        return xyDepositMapper.insertXyDeposit(xyDeposit);
    }

    /**
     * 修改会员-余额提现
     *
     * @param xyDeposit 会员-余额提现
     * @return 结果
     */
    @Override
    public int updateXyDeposit(XyDeposit xyDeposit) {
        return xyDepositMapper.updateXyDeposit(xyDeposit);
    }

    /**
     * 批量删除会员-余额提现
     *
     * @param ids 需要删除的会员-余额提现主键
     * @return 结果
     */
    @Override
    public int deleteXyDepositByIds(String[] ids) {
        return xyDepositMapper.deleteXyDepositByIds(ids);
    }

    /**
     * 删除会员-余额提现信息
     *
     * @param id 会员-余额提现主键
     * @return 结果
     */
    @Override
    public int deleteXyDepositById(String id) {
        return xyDepositMapper.deleteXyDepositById(id);
    }

    @Override
    @Transactional
    public AjaxResult withdraw(XyDepositDto xyDepositDto) {


        XyBankinfo bankinfo = bankinfoService.selectXyBankinfoByUId(xyDepositDto.getUid() + "");
        if (null == bankinfo) {
            return AjaxResult.error(102, "请维护收款账户信息");
        }
        XyUsers xyUsers = xyUsersService.selectXyUsersById(xyDepositDto.getUid() + "");

        String pwd2 = xyDepositDto.getPwd2();
        if (StringUtils.isEmpty(pwd2) || !pwd2.equals(xyUsers.getPwd2())) {
            return AjaxResult.error(103, "提现密码不对");
        }

        if (xyUsers.getBalance().compareTo(xyDepositDto.getNum()) < 0) {
            return AjaxResult.error(101, "用户余额不足");
        }

        //生成订单号
        String depositSn = JksdUtils.buildSn("CO");
        XyDeposit xyDeposit = new XyDeposit();
        xyDeposit.setNum(xyDepositDto.getNum());
        xyDeposit.setUid(xyDepositDto.getUid());

        xyDeposit.setBank(bankinfo.getBankname());
        xyDeposit.setCardNo(bankinfo.getCardnum());
        xyDeposit.setId(depositSn);
        xyDeposit.setRealNum(xyDeposit.getNum());
        xyDeposit.setBkId(bankinfo.getId());
        xyDeposit.setShouxu("0");
        xyDeposit.setStatus(1);//待审核
        xyDeposit.setType("wallet");
        xyDeposit.setUlang(xyUsers.getUlang());
        xyDeposit.setAddtime(System.currentTimeMillis() / 1000);
        //添加balanceLog
//        'uid' => $uid,
//                'oid' => $id,
//                'num' => $num,
//                'type' => 7, //TODO 7提现
//                'status' => 2,
//                'addtime' => time(),
        XyBalanceLog balanceLog = new XyBalanceLog();
        balanceLog.setUid(xyUsers.getId());
        balanceLog.setOid(xyDeposit.getId());
        balanceLog.setNum(xyDeposit.getNum());
        balanceLog.setStatus(2L);
        balanceLog.setAddtime(System.currentTimeMillis() / 1000);
        balanceLog.setType(7L);//提现

        xyBalanceLogService.insertXyBalanceLog(balanceLog);
        xyDepositMapper.insertXyDeposit(xyDeposit);
        xyUsersService.updateXyUsers(xyUsers);

        return AjaxResult.success(xyUsers);
    }

    @Override
    @Transactional
    public AjaxResult approvalWithDraw(XyDeposit xyDeposit) {
//        @Excel(name = "订单状态 1待处理 2审核通过 3审核不通过,4已打款")
        Long uid = xyDeposit.getUid();
        XyUsers xyUsers = xyUsersService.selectXyUsersById(uid + "");
//        订单状态 1待处理 2审核通过 3审核不通过,4已打款
        if (2 == xyDeposit.getStatus()) {
            BigDecimal newBalance = xyUsers.getBalance().subtract(xyDeposit.getNum());
            xyUsers.setBalance(newBalance);
        }
        xyDeposit.setEndtime(System.currentTimeMillis() / 1000);
        xyDepositMapper.updateXyDeposit(xyDeposit);
        xyUsersService.updateXyUsers(xyUsers);

        return AjaxResult.success();
    }


}
